package com.dyzx.hbb.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;

import com.dyzx.hbb.modules.sys.entity.SysMenu;
import com.dyzx.hbb.modules.sys.service.SysMenuService;
import com.dyzx.hbb.modules.sys.mapper.SysMenuMapper;
import com.dyzx.hbb.modules.sys.service.SysRoleMenuService;
import com.dyzx.hbb.modules.sys.service.SysRoleUserService;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【sys_menu(角菜单表)】的数据库操作Service实现
* @createDate 2025-04-14 13:29:08
*/
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
    implements SysMenuService{
    @Autowired
    private SysRoleMenuService roleMenuService;

    @Override
    public List<Map<String, Object>> findByTreeList() {
        // 1. 查询所有菜单列表
        List<SysMenu> menuList = this.list();

        // 2. 构建树形结构
        List<Map<String, Object>> treeList = menuList.stream()
            .filter(menu -> menu.getParentId() == 0)  // 获取顶级菜单
            .map(menu -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", menu.getId());
                map.put("name", menu.getName());
                map.put("icon", menu.getIcon());
                map.put("type", menu.getType());
                map.put("parent_id", menu.getParentId());
                map.put("perms_key", menu.getPermsKey());
                map.put("route_path", menu.getRoutePath());
                map.put("comp_path", menu.getCompPath());
                map.put("params", menu.getParams());
                map.put("idx", menu.getIdx());
                map.put("status", menu.getStatus());

                map.put("children", getChildren(menu, menuList));
                return map;
            })
            .collect(Collectors.toList());

        return treeList;
    }

    //Redis 缓存已保存，直接从缓存中获取
    @Cacheable(value = "menu:miniList",key = "#organId+':'+#userId")
    @Override
    public List<Map<String, Object>> findByTreeMiniList(Long organId, Long userId) {
        boolean admin = SecurityContextHolder.isNotAdmin();


        MyLambdaQueryWrapper<SysMenu> queryWrapper=new MyLambdaQueryWrapper<>();
        if(admin){

            List<Long> menuIds = roleMenuService.getMenuIdsByUserId(userId);
            queryWrapper.in(SysMenu::getId,menuIds);
        }
        // 1. 查询所有菜单列表
        List<SysMenu> menuList = this.list(queryWrapper);

        // 2. 构建简化的树形结构
        // 获取顶级菜单
        //    map.put("children", getMiniChildren(menu, menuList));

        return menuList.stream()
            .filter(menu -> menu.getParentId() == 0)  // 获取顶级菜单
            .map(menu -> {
                Map<String, Object> map = new HashMap<>();

                map.put("id", menu.getId());
                map.put("name", menu.getName());
                map.put("icon", menu.getIcon());
                map.put("perms_key", menu.getPermsKey());

                map.put("route_path", menu.getRoutePath());
                map.put("comp_path", menu.getCompPath());

                    List<Map<String, Object>> miniChildren = getMiniChildren(menu, menuList);
                if(!miniChildren.isEmpty()){
                    map.put("children", miniChildren);
                }
                //    map.put("children", getMiniChildren(menu, menuList));


                return map;
            })
            .collect(Collectors.toList());
    }


    @Override
    public List<Map<String, Object>> findByTreeRouterList() {
        MyLambdaQueryWrapper<SysMenu> queryWrapper=new MyLambdaQueryWrapper<>();
        queryWrapper.le(SysMenu::getType,2);
        // 1. 查询所有菜单列表
        List<SysMenu> menuList = this.list(queryWrapper);

        // 2. 构建简化的树形结构
        List<Map<String, Object>> treeList = menuList.stream()
                .filter(menu -> menu.getParentId() == 0)  // 获取顶级菜单
                .map(menu -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", menu.getId());
                    map.put("name", menu.getName());
                    map.put("icon", menu.getIcon());
                    map.put("perms_key", menu.getPermsKey());
                    map.put("path", menu.getRoutePath());
                    map.put("comp_path", menu.getCompPath());

                    List<Map<String, Object>> miniChildren = getMiniRouterChildren(menu, menuList);
                    if(!miniChildren.isEmpty()){
                        map.put("routes", miniChildren);
                    }
                    //    map.put("children", getMiniChildren(menu, menuList));


                    return map;
                })
                .collect(Collectors.toList());

        return treeList;
    }
    private List<Map<String, Object>> getMiniRouterChildren(SysMenu menu, List<SysMenu> menuList) {
        // 递归查找子菜单,返回简化的树形结构
        return menuList.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))  // 过滤出当前菜单的子菜单
                .map(m -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", m.getId());
                    map.put("name", m.getName());
                    map.put("icon", m.getIcon());
                    map.put("perms_key", m.getPermsKey());
                    map.put("path", m.getRoutePath());
                    map.put("comp_path", m.getCompPath());
                    // 递归获取子菜单
                    List<Map<String, Object>> children = getMiniRouterChildren(m, menuList);
                    if (!children.isEmpty() &&  children.size() > 0) {
                        map.put("routes", children);
                    }
                    return map;
                })
                .collect(Collectors.toList());
    }



    private List<Map<String, Object>> getMiniChildren(SysMenu menu, List<SysMenu> menuList) {
        // 递归查找子菜单,返回简化的树形结构
        return menuList.stream()
            .filter(m -> m.getParentId().equals(menu.getId()))  // 过滤出当前菜单的子菜单
            .map(m -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", m.getId());
                map.put("name", m.getName());
                map.put("icon", m.getIcon());
                map.put("perms_key", m.getPermsKey());

                map.put("route_path", m.getRoutePath());
                map.put("comp_path", m.getCompPath());
                // 递归获取子菜单
                List<Map<String, Object>> children = getMiniChildren(m, menuList);
                if (!children.isEmpty() &&  children.size() > 0) {
                    map.put("children", children);
                }
                return map;
            })
            .collect(Collectors.toList());
    }

    private List<Map<String, Object>> getChildren(SysMenu menu, List<SysMenu> menuList) {
        // 递归查找子菜单
        return menuList.stream()
            .filter(m -> m.getParentId().equals(menu.getId()))  // 过滤出当前菜单的子菜单
            .map(m -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", m.getId());
                map.put("name", m.getName());
                map.put("icon", m.getIcon());
                map.put("type", m.getType());
                map.put("parent_id", m.getParentId());
                map.put("perms_key", m.getPermsKey());
                map.put("route_path", m.getRoutePath());
                map.put("comp_path", m.getCompPath());
                map.put("params", m.getParams());
                map.put("idx", m.getIdx());
                map.put("status", m.getStatus());
                // 递归获取子菜单
                List<Map<String, Object>> children = getChildren(m, menuList);
                if (!children.isEmpty()) {
                    map.put("children", children);
                }
                return map;
            })
            .collect(Collectors.toList());
    }
}




